<?php
/**
 * Created by PhpStorm.
 * User: dengsitan
 * Date: 16/5/18
 * Time: 下午2:56
 */

namespace Admin\Controller;


Class ComplaintController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
    }


    /**
     * 投诉管理
     * @param order_id
     * @param user
     * @param master
     */

    public function Manage(){
        $order_id = I('get.order_id','');
        $user = I('get.user','');
        $master = I('get.master','');
        $start_time = I('get.start_time','');
        $end_time = I('get.end_time','');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);

        if($order_id){
            $maps['rep.repair_order_num_alias'] = array('like',"%$order_id%");
        }
        if($user){
            $maps['usr.user_name'] = array('like',"%$user%");
        }
        if($master){
            $maps['inf.id_name'] = array('like',"%$master%");
        }
        if($start_time && $end_time){
            $map['com.add_time'] = array('between',$start_time.','.$end_time);
        }elseif($start_time){
            $map['com.add_time'] = array('egt',$start_time);
        }
        $count = M()->table('zsf_repair_order_complaint com')->field('com.*,rep.repair_order_num_alias,rep.category_name,rep.fault_id_list,usr.user_name,usr.phone as user_phone,inf.id_name as master_name,rep.worker_tel as master_phone')
            ->join("LEFT JOIN zsf_repair_order rep on rep.repair_order_id = com.order_id")
            ->join("LEFT JOIN zsf_user usr on usr.user_id = rep.user_id")
            ->join("LEFT JOIN zsf_user_real_info inf on inf.user_id = rep.worker_user_id")
            ->where($maps)
            ->count();
        if($count){
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出

        $data = M()->table('zsf_repair_order_complaint com')->field('com.*,rep.repair_order_num_alias,rep.category_name,rep.fault_id_list,usr.user_name,usr.phone as user_phone,inf.id_name as master_name,rep.worker_tel as master_phone')
                ->join("LEFT JOIN zsf_repair_order rep on rep.repair_order_id = com.order_id")
                ->join("LEFT JOIN zsf_user usr on usr.user_id = rep.user_id")
                ->join("LEFT JOIN zsf_user_real_info inf on inf.user_id = rep.worker_user_id")
                ->where($maps)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
            foreach($data as $k=>$v){
                $data[$k]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                $Fault['fault_id'] = array('in',$v['fault_id_list']);
                $FaultResult = M('fault')->field('name')->where($Fault)->select();
                $FaultResult = array_column($FaultResult,'name');
                $FaultName = implode(',',$FaultResult);
                $data[$k]['Fault_name'] = $FaultName;
                $data[$k]['complaint'] = $v['reason'].' '.$v['content'];
            }
        }else{
            $data = array();
        }


        $this->breadcrumb1='投诉管理';
        $this->breadcrumb2='投诉管理';
        $this->url=U('Complaint/Manage');
        $this->assign('page',$show);
        $this->assign('Complaint',$data);
        $this->display();
    }

    /**
     * 反馈管理
     * @param contact
     */

    public function Feedback(){
        $contact = I('get.contact','');
        $start_time = I('get.start_time','');
        $end_time = I('get.end_time','');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);

        if($contact){
            $maps['contact'] = array('like',"%$contact%");
        }
        if($start_time && $end_time){
            $map['add_time'] = array('between',$start_time.','.$end_time);
        }elseif($start_time){
            $map['add_time'] = array('egt',$start_time);
        }

        $count = M('user_advise')->where($maps)->count();
        if($count) {
            $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show = $Page->show();// 分页显示输出

            $data = M('user_advise')->where($maps)->limit($Page->firstRow . ',' . $Page->listRows)->select();

            foreach ($data as $k => $v) {
                $data[$k]['add_time'] = date('Y-m-d H:i:s', $data[$k]['add_time']);
            }
        }else{
            $data = array();
        }
        $this->breadcrumb1='投诉管理';
        $this->breadcrumb2='问题反馈';
        $this->url=U('Complaint/Feedback');
        $this->assign('page',$show);
        $this->assign('feedback',$data);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();
    }

    /**
     * 删除反馈
     */

    public function Delfeedback(){
        $id = I('post.Del');
        $id = implode(',',$id);
        $data = M('user_advise')->where("id in ($id)")->delete();
        if($data){
            operatelog('反馈管理','2',$id);
            echo 1;
        }else{
            echo 2;
        }

    }

    /**
     * 回访记录
     */

    public function visit(){
        $advise_id = I('get.advise_id');
        $complaint_id = I('get.complaint_id');
        if($advise_id){
            $info = M('user_advise_visit')->where("advise_id = $advise_id and status = 1")->select();
             if($info) {
                 foreach ($info as $k => $v) {
                     $info[$k]['add_time'] = date('Y-m-d H:i:s', $info[$k]['add_time']);
                     if ($info[$k]['result'] == '1') {
                         $info[$k]['result'] = '继续回访';
                     } else {
                         $info[$k]['result'] = '处理完毕';
                     }
                 }
             }
            $this->breadcrumb1='投诉管理';
            $this->breadcrumb2='问题反馈';
            $this->breadcrumb3='回访记录';
            $this->url2=U('Complaint/Feedback');
            $this->assign('advise',$info);
            $this->display('visit');
        }else{
            $data = M('user_advise_visit')->where("advise_id = $complaint_id and status = 2")->select();
            if($data){
                foreach($data as $k=>$v){
                    $data[$k]['add_time'] = date('Y-m-d H:i:s', $data[$k]['add_time']);
                    if ($data[$k]['result'] == '1') {
                        $data[$k]['result'] = '继续回访';
                    } else {
                        $data[$k]['result'] = '处理完毕';
                    }
                }
            }

            $this->breadcrumb1='投诉管理';
            $this->breadcrumb2='投诉管理';
            $this->breadcrumb3='回访记录';
            $this->url2=U('Complaint/Manage');
            $this->assign('complaint',$data);
            $this->display('complaint');
        }

    }

    /**
     * 添加回访记录
     */

    public function addReturnVisit(){
        if(IS_POST){
            $data['advise_id'] = I('post.advise_id');
            $data['record'] = I('post.record');
            $data['visitor'] = I('post.visitor');
            $data['result'] = I('post.result');
            $data['status'] =I('post.status');
            $data['add_time'] = time();
            $info = M('user_advise_visit')->add($data);
            if($info){
                operatelog('反馈管理','1',$info);
                echo 1;
            }else{
                echo 2;
            }
        }
    }

}